package com.example.myandroidapp1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.myandroidapp1.dto.ImageLoader;
import com.example.myandroidapp1.dto.MyGallery;

public class GridViewImage extends Activity implements OnClickListener {

	private ProgressBar progressBar;
	private ImageView imageView;
	private TextView textView;

	private Button leftButton;
	private Button rightButton;

	private MyGallery myGallery;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gridviewimage);
		initialize();
		// new
		// ImageLoader(imageView,progressBar,textView).execute(gridItem.getImageUrl());
		new ImageLoader(imageView, progressBar, textView).execute(myGallery.getImageList().get(myGallery.getCurrentPosition()).getImageUrl());
		leftButton.setOnClickListener(this);
		rightButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		progressBar.setVisibility(View.VISIBLE);
		
		switch (v.getId()) {
		case R.id.bLeftImage:
			myGallery.setCurrentPosition(myGallery.getCurrentPosition() - 1);
			new ImageLoader(imageView, progressBar, textView).execute(myGallery.getImageList().get(myGallery.getCurrentPosition()).getImageUrl());
			if (myGallery.getCurrentPosition() == 0) {
				leftButton.setVisibility(View.INVISIBLE);
			}
			rightButton.setVisibility(View.VISIBLE);

			break;
		case R.id.bRightImage:
			myGallery.setCurrentPosition(myGallery.getCurrentPosition() + 1);
			new ImageLoader(imageView, progressBar, textView).execute(myGallery.getImageList().get(myGallery.getCurrentPosition()).getImageUrl());
			if (myGallery.getCurrentPosition() == myGallery.getImageList().size() - 1) {
				rightButton.setVisibility(View.INVISIBLE);
			}
			leftButton.setVisibility(View.VISIBLE);

			break;
		default:
			break;
		}
	}

	private void initialize() {
		progressBar = (ProgressBar) findViewById(R.id.pbFullImage);
		imageView = (ImageView) findViewById(R.id.ivFullImage);
		textView = (TextView) findViewById(R.id.tvFullImageHidden);
		leftButton = (Button) findViewById(R.id.bLeftImage);
		rightButton = (Button) findViewById(R.id.bRightImage);

		Bundle bundle = getIntent().getExtras();
		myGallery = (MyGallery) bundle.getSerializable("myGallery");
		if (myGallery.getCurrentPosition() == 0) {
			leftButton.setVisibility(View.INVISIBLE);
		}
		if (myGallery.getCurrentPosition() == myGallery.getImageList().size() - 1) {
			rightButton.setVisibility(View.INVISIBLE);
		}

	}
}
